package com.example.myapplication.slice.slice;


import ohos.media.audio.AudioCapturer;
import ohos.media.audio.AudioCapturerInfo;
import ohos.media.audio.AudioStreamInfo;
import ohos.media.audio.SoundEffect;

import java.util.UUID;

public class AudioCaptureUtils {
    private AudioStreamInfo audioStreamInfo;
    private AudioCapturer audioCapturer;
    private AudioCapturerInfo audioCapturerInfo;

    // channelMask 声道
    // SampleRate 频率
    public AudioCaptureUtils(AudioStreamInfo.ChannelMask channelMask, int SampleRate) {
        this.audioStreamInfo = new AudioStreamInfo.Builder()
                .encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT)
                .channelMask(channelMask)
                .sampleRate(SampleRate)
                .build();
        this.audioCapturerInfo = new AudioCapturerInfo.Builder().audioStreamInfo(audioStreamInfo).build();
    }

    // 其他方法或逻辑可以在这里添加

    // 例如，获取audioStreamInfo的方法
    public AudioStreamInfo getAudioStreamInfo() {
        return audioStreamInfo;
    }

    // 例如，获取audioCapturerInfo的方法
    public AudioCapturerInfo getAudioCapturerInfo() {return audioCapturerInfo;}

    //packageName 包名
    //packageName 包名
    public void init(String packageName) {
        this.init(SoundEffect.SOUND_EFFECT_TYPE_NS,packageName );
    }
    //soundEffect 音效uuid

//packageName 包名
    public void init(UUID soundEffect, String packageName) {
        if (audioCapturer == null || audioCapturer.getState() == AudioCapturer.State.STATE_UNINITIALIZED)
            audioCapturer = new AudioCapturer(this.audioCapturerInfo);
        audioCapturer.addSoundEffect(soundEffect, packageName);
    }

    public void stop(){
        this.audioCapturer.stop();
    }

    public void destroy(){
        this.audioCapturer.stop();
        this.audioCapturer.release();
    }

    public Boolean start() {
        if (audioCapturer == null)
            return false;
        return audioCapturer.start();
    }

    //buffers 需要写入的数据流
//offset 数据流的偏移量
//byteslength 数据流的长度
    public int read(byte[] buffers, int offset, int bytesLength){
        return audioCapturer.read(buffers,offset,bytesLength);
    }

    //获取AudioCapturer的实例audioCapturer
    public AudioCapturer get(){
        return this.audioCapturer;
    }
}

